<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\SendMessage;
use App\Models\UsersChatList;
use Encore\Admin\Controllers\AdminController;
use App\Models\User;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class UsersController extends AdminController
{
    protected $title = '用户管理';
    /**
     * 网站用户列表
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        // 过滤系统用户
        $grid->model()->where('id', '<>', 25);

        $grid->column('id', 'ID');
        $grid->column('name', '昵称');
        $grid->column('email', '邮箱');
        $grid->column('created_at', '创建时间');
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            // 去掉编辑
            $actions->disableEdit();
            $actions->disableDelete();
            $actions->add(new SendMessage());
        });

        return $grid;
    }

    /**
     * 用户详情
     *
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', 'ID');
        $show->field('name', '昵称');
        $show->field('email', '邮箱');
        $show->field('created_at');
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
            });
        return $show;
    }

    public function message(Request $request)
    {
        $userId = $request->input('user_id');
        $user = User::find($userId);

        UsersChatList::updateOrCreate(
            ['user_id' => '25', 'friend_id' => $userId],
            ['status' => 1]
        );
        return view('admin.user.message', compact('user'));
    }
}